package com.zhaoyue.study.zk;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2017/2/14
 * Time: 11:29
 * To change this template use File | Settings | File Templates.
 */
public class CuratorConsumer {




    public static void main(String[] args) {

        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3); //会话重试策略

        CuratorFramework framework = CuratorFrameworkFactory
                .builder()
                .connectString("192.168.56.56:2181")
                .connectionTimeoutMs(5000)
                .retryPolicy(retryPolicy)
                .build();

        framework.start();

        //创建
        try{
            //创建临时节点
            framework.create().withMode(CreateMode.EPHEMERAL).forPath("/temp","aaa".getBytes());
            //临时顺序节点
            framework.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/temp_sq","bbb".getBytes());
            //永久节点
            framework.create().withMode(CreateMode.EPHEMERAL).forPath("/study",null);
            //creatingParentsIfNeeded 如果父节点不存在在创建
            String result = framework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath("/aa/bb","aa".getBytes());
            System.out.println("result:" + result);

            //监听节点事件
            final NodeCache nodeCache = new NodeCache(framework, "/temp");
            nodeCache.start();
            nodeCache.getListenable().addListener(new NodeCacheListener() {
                @Override
                public void nodeChanged() throws Exception {
                    System.out.println("new:" + new String(nodeCache.getCurrentData().getData()));
                }
            });

            //监听目录下的子节点
            PathChildrenCache pathChildrenCache = new PathChildrenCache(framework,"/aa",true);
            pathChildrenCache.start();
            pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
                @Override
                public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                    System.out.println("开始事件分析");
                    ChildData childData = event.getData();
                    System.out.println("type:"+event.getType().name());
                    System.out.println("path:"+childData.getPath());
                    System.out.println("data:"+new String(childData.getData()));

                    List<String> list = client.getChildren().forPath("/aa");
                    for (String str : list) {
                        System.out.println("childRen:" + str);
                    }
                }
            });


            //可重入互斥锁
//            InterProcessMutex lock = new InterProcessMutex(framework,"/lock");
//            lock.acquire();//获取锁
//            lock.release();//释放锁

            //读写锁







            Thread.sleep(Integer.MAX_VALUE);

        }catch (Exception e){
            e.printStackTrace();
        }




    }
}
